import { defHttp } from '/@/utils/http/axios';
import {
  LoginParams,
  LoginResultModel,
  GetUserInfoModel,
  GetCaptchaModel,
  PasswordParams,
} from './model/userModel';

import { ErrorMessageMode } from '/#/axios';

enum Api {
  GetCaptcha = '/auth/oauth/captcha/img',
  Login = '/auth/oauth/token',
  Logout = '/auth/oauth/logout',
  GetPermCode = '/auth/getPermCode',
  GetUserInfo = '/system/user/info',
  CHANGE_PASSWORD_URL = '/system/user/updatePassword',
  TestRetry = '/system/user/updatePassword',
}

/**
 * @description: user login api
 */
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<LoginResultModel>(
    {
      url: Api.Login,
      params,
      headers: {
        // 客户端信息加密摘要认证，明文：lemon-cloud-web:123456
        Authorization: 'Basic bGVtb24tY2xvdWQtd2ViOjEyMzQ1Ng==',
      },
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: change user password api
 */
export function changePasswordApi(params: PasswordParams, mode: ErrorMessageMode = 'message') {
  return defHttp.post(
    {
      url: Api.CHANGE_PASSWORD_URL,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: getUserInfo
 */
export function getUserInfo() {
  return defHttp.get<GetUserInfoModel>({ url: Api.GetUserInfo }, { errorMessageMode: 'none' });
}

export function getPermCode() {
  return defHttp.get<string[]>({ url: Api.GetPermCode });
}

export function doLogout() {
  return defHttp.post({ url: Api.Logout });
}

export function getCaptcha(params) {
  return defHttp.get<GetCaptchaModel>({ url: Api.GetCaptcha, params });
}

export function getImgCaptcha() {
  return defHttp.get<GetCaptchaModel>({ url: Api.GetCaptcha });
}

export function testRetry() {
  return defHttp.get(
    { url: Api.TestRetry },
    {
      retryRequest: {
        isOpenRetry: true,
        count: 5,
        waitTime: 1000,
      },
    },
  );
}
